package manager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;

import algorithm.algorithm;

import objects.Mobile;
import objects.Station;

public class Manager {

	private static Station station1 = new Station();

	// Lancemenent des objects
	public static ArrayList<Mobile> ListMobile = new ArrayList<Mobile>();
	public static int subcarrierRequested = 0;
	public static int numbersubcarrieravailable = Station.numberSubcarrier;
	
	public static Mobile addMobile(Station s){
			Mobile m = new Mobile();
			m.setRatePerSymbol(1);
			System.out.println("iii A phone connects ..."  ) ;
			System.out.println("<<< give a name to your Mobile : ") ;
			m.name = getInput();

			System.out.println("<<< Enter the flow need in Kbit/s :") ;
			//enregistre dans la bande passante le nombre de frequence alloué

			//Increment station number of connected phone
			s.numberofphone+=1;

			//Save the BW needs
			m.setBandWidthNeeds(Integer.parseInt(getInput()));
			
			//save name of the station connected on
			m.station = s.name;
			
			//Compute the number of subcarrier needs at one timeslot
			m.setNbTweSubNeed(convertBandwidthToNbTweSubNeed(m.getBandWidthNeeds()));
			subcarrierRequested += m.getNbTweSubNeed() ;
		
			//set station parameters
			m.etranBandAllocation = s.etranBandAllocation ;
			m.etranBandAllocationRate = s.etranBandAllocationRate ;

		return m;
	}
	private static int convertBandwidthToNbTweSubNeed(int i){
		//      1+(int) (m.getBandWidthNeeds()/(station1.getRatePerSubcarrier())/12 )
		return (1+(int) ((i/(station1.getRatePerSubcarrier()))/12) );
	}
	public static int convertNbTweSubNeedToBandwidth(int i){
		return  (i ) * 12 * (int) (station1.getRatePerSubcarrier());
	}
	
	private static void modifyMobile() {
		AfficheMobiles();

		if (! ListMobile.isEmpty()){
			System.out.println("<<< Which mobile would you like to change the bandwidth needs: ") ;
			String modify=getInput();
			Iterator<Mobile> it = ListMobile.iterator();

			Mobile o = null ;
			while (it.hasNext()){
				o = (Mobile) it.next();
				
				if(o.name.toString().compareTo(modify) == 0){
					System.out.println("<<< " + subcarrierRequested + " Enter the new bandwidth : ") ;
					//remove old demand
					subcarrierRequested -= o.getNbTweSubNeed() ;
					o.setBandWidthNeeds(Integer.parseInt(getInput()));
					o.setNbTweSubNeed(convertBandwidthToNbTweSubNeed(o.getBandWidthNeeds()));
					//add new demand
					subcarrierRequested += o.getNbTweSubNeed() ;
					System.out.println("<<< " + subcarrierRequested + " Enter the new bandwidth : ") ;
					break;
				}
				else
					System.out.println("!!! Phone not find, you selected : " + modify);
			}
		}else System.out.println("!!! No phone connected !");
	}
	public static void disconnectMobile(Station s){
		System.out.println("<<< What mobile do you want to disconnect ?") ;
		AfficheMobiles();
		Mobile o = null ;
		
		int delcheck = 0 ;
		do{
			System.out.println("<<<< Entrer the name of mobile, witch you want to diconnect :") ;

			String del=getInput();
			Iterator<Mobile> it = ListMobile.iterator();
			while (it.hasNext() ){
				o = (Mobile) it.next();
				if(o.name.toString().compareTo(del) == 0){
					
					//Decrement station number of connected phone
					s.numberofphone-=1;
					ListMobile.remove(o);

					delcheck=1;
					break;
				}
				else
					System.out.println("!!! Error in delete, you have enter : " + del);
			}
		}while(delcheck==0) ;
		
		//	algorithm.compute();
		System.out.println("A mobile is disconnected from the network...") ;
		subcarrierRequested -= o.getNbTweSubNeed() ;	
		//appeler l'algo
	}
	
	public static void AfficheStationState(Station s){
		System.out.println(" ---------------------------------- " ) ;
		System.out.println("Station status : " ) ;
		
		System.out.println("  Upload Band Eutran :                    " + s.uploadEtranBandLimit[0]+"Mhz - " + s.uploadEtranBandLimit[1] +"Mhz") ;
		System.out.println("  Download Band Eutran  :                 " + s.downloadEtranBandLimit[0]+"Mhz - "+ s.downloadEtranBandLimit[1]  +"Mhz" ) ;
		System.out.println("  Effective lower Download Band Eutran  : " + s.selectedLowerDownloadEtranBandLimit);
		System.out.println("  Effective lower Download Band Eutran  : " + s.selectedHigherDownloadEtranBandLimit);
		System.out.println("  Bandwith  :                             " + s.etranBandAllocation +" Mhz" ) ;
		System.out.println("  Upload Band Eutran :                    " + s.etranBandAllocationRate +" Mbit") ;
		System.out.println("  Space between two sub-carrier :         " + s.ratePerSubcarrier+" Kbit") ;
		System.out.println("  Total number of sub-carrier :           " + s.numberSubcarrier ) ;
		System.out.println("  Total number of sub-carrier available : " + numbersubcarrieravailable ) ;
		System.out.println("  Total number of mobile connected :      " + s.numberofphone) ;
		
	}
	public static void AfficheMobiles(){
		
		//execution de algorithme
		
		Iterator<Mobile> it = ListMobile.iterator();
		if ( ! ListMobile.isEmpty() ){
			System.out.println(" ---------------------------------- " ) ;
			System.out.println("Mobile connected : " ) ;
			while (it.hasNext() ){
				Mobile o = (Mobile) it.next();
				System.out.println("-------------- Phone "+ o.name + " ------------ " ) ;
				System.out.println("Station :				"+ o.station ) ;
				System.out.println("  Etran Band :         	" + o.etranBandAllocation + "Mhz") ;
				System.out.println("  Etran Band rate :    	" + o.etranBandAllocationRate + "Mbits") ;
				System.out.println("Phone characteristics :	") ;
				System.out.println("  Sub-carriers / RB need :     	" + o.getNbTweSubNeed()*12 + " time Slot -> " + o.getNbTweSubNeed() + " Ressource blocks") ;
				System.out.println("  Sub-carriers / RB got :	" + o.getReceivedFrequency() *12 + " time slot -> "+ o.getReceivedFrequency() + " Ressource blocks") ;
				System.out.println("  Rate Per :		" + o.getRatePerSymbol() ) ;
				System.out.println("  Bandwidth Needed :	" + o.getBandWidthNeeds() + "Kbits") ;
				System.out.println("  Bandwidth Got:    	" + o.getBandWidthGot() + "Kbits") ;
			}
		}
		else{
			System.out.println(" ---------------------------------- " ) ;
			System.out.println("No mobile connected.");
		}
	}

	public static String getInput(){
		
		Scanner in = new Scanner(System.in);
		String choice = in.next();
		return choice;
	}
	
	public static void main(String[] args) {

		boolean run = true;
		while(run){
			
			algorithm.compute();
			
			System.out.println("------------------------------------") ;
			System.out.println("Take one option :					") ;
			System.out.println("1 - View the status of the station	") ;
			System.out.println("2 - Put a mobile					") ;
			System.out.println("3 - Modify a mobile					") ;
			System.out.println("4 - Disconnect a mobile				") ;
			System.out.println("5 - View the status of mobiles		") ;
			System.out.println("6 - Destroy the station				") ;
			System.out.println("____________________________________") ;
			String choice = getInput() ;
			
			if (choice.compareTo("1") == 0){
				 AfficheStationState(station1);
			 }
			 else if  ( choice.compareTo("2") == 0){
				 ListMobile.add(addMobile(station1));
				 //sort the list the greater before
				// Collections.sort(ListMobile, Collections.reverseOrder());
			 }else if  ( choice.compareTo("3") == 0 ){
				 modifyMobile();
			 }else if  ( choice.compareTo("4") == 0 ){
				 disconnectMobile(station1);
			 }else if  ( choice.compareTo("5") == 0 ){
				 AfficheMobiles();
			 }else if  ( choice.compareTo("6") == 0 ){
				System.out.println("Destroyed !") ;
				run = false;
			}
		}
	}
}
